package com.liuanbang.saro.sarodemo.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.ViewFlipper;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.bumptech.glide.Glide;
import com.liuanbang.saro.sarodemo.R;
import com.liuanbang.saro.sarodemo.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ViewFlipperActivity extends BaseActivity {

    @Bind(R.id.viewFlipper_flipper_view)
    ViewFlipper viewFlipperFlipperView;
    @Bind(R.id.seekBar_flipper_view)
    SeekBar seekBarFlipperView;
    @Bind(R.id.convenientBanner_flipper_view)
    ConvenientBanner convenientBannerFlipperView;
    private Context mContext = this;
    private String[] bitmapImageUrls = new String[]{"http://c.hiphotos.baidu.com/image/h%3D360/sign=21a755bfa41ea8d395227202a70b30cf/43a7d933c895d143bf16062771f082025aaf0755.jpg"
            , "http://a.hiphotos.baidu.com/image/h%3D360/sign=e9cda7a0f403738dc14a0a24831ab073/08f790529822720e5c3f3afa79cb0a46f21fab39.jpg",
            "http://e.hiphotos.baidu.com/image/h%3D360/sign=011f56749058d109dbe3afb4e158ccd0/b7fd5266d016092477539711d60735fae6cd3441.jpg",
            "http://e.hiphotos.baidu.com/image/h%3D360/sign=69ea2a2f0cf41bd5c553eef261db81a0/f9198618367adab45e19d64789d4b31c8601e4ed.jpg"};

    private List<String> localImages = new ArrayList<>();

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_view_flipper);
        ButterKnife.bind(this);

        //为ImageView添加
        for (int i = 0; i < 4; i++) {
            ImageView imageView = getImageViewLoadURL(bitmapImageUrls[i]);
            viewFlipperFlipperView.addView(imageView);
            localImages.add(bitmapImageUrls[i]);
        }

        //设置ViewFlipper的属性
        viewFlipperFlipperView.setInAnimation(mContext, R.anim.in_left2right);
        viewFlipperFlipperView.setOutAnimation(mContext, R.anim.out_botom2top);
        //设置页面切换的间隔时间:1000毫秒
        viewFlipperFlipperView.setFlipInterval(3000);
        //设置自动开始图片切换效果
        viewFlipperFlipperView.setAutoStart(true);

        //seekBar监听器
        seekBarFlipperView.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                viewFlipperFlipperView.setFlipInterval(progress * 100);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        // initGesture();

        initBanner();
        convenientBannerFlipperView.startTurning(3000);
        convenientBannerFlipperView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                showToast(position + "");
            }
        });
    }

    private void initBanner() {
        //自定义你的Holder，实现更多复杂的界面，不一定是图片翻页，其他任何控件翻页亦可。
        convenientBannerFlipperView.setPages(
                new CBViewHolderCreator<LocalImageHolderView>() {
                    @Override
                    public LocalImageHolderView createHolder() {
                        return new LocalImageHolderView();
                    }
                }, localImages)
                //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                .setPageIndicator(new int[]{R.drawable.round1, R.drawable.round2})
                //设置指示器的方向
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
        //设置翻页的效果，不需要翻页效果可用不设  集成特效之后会有白屏现象，新版已经分离，如果要集成特效的例子可以看Demo的点击响应。
        //.setPageTransformer(Transformer.DefaultTransformer);
//        convenientBanner.setManualPageable(false);//设置不能手动影响


    }

  /*  private void initGesture() {
        for (int i = 0; i < 4; i++) {

            ImageView iv = new ImageView(mContext);
            ImageView indicatorView = new ImageView(mContext);
            if (i == 0) {
                indicatorView.setImageResource(R.drawable.page_selected);//初始默认选中第一张为当前指引
            } else {
                indicatorView.setImageResource(R.drawable.page_normal);
            }
            iv.setImageResource(fliperImage[i]);
            //把指引条的图片添加到LinearLayout里面
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.setMargins(6, 0, 0, 0);
            indicatorView.setLayoutParams(lp);
            iv.setScaleType(ScaleType.FIT_XY);
            gestureFliperView.addView(iv, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            mImageView[i] = indicatorView;
            indicatorLayout.addView(mImageView[i]);
        }
        gestureFliperView.setAutoStart(true);    //让viewFlipper自动播放
        gestureFliperView.setFlipInterval(4000); //间隔为4S
        if (gestureFliperView.isAutoStart() && viewFilper.isFlipping()) {
            gestureFliperView.startFlipping();
        }
        if (gestureFliperView.getDisplayedChild() != 0) {
            gestureFliperView.setInAnimation(AnimationUtils.loadAnimation(
                    mContext, R.anim.m_push_up_in));   //进出动画
            gestureFliperView.setOutAnimation(AnimationUtils.loadAnimation(
                    mContext, R.anim.m_push_up_out));
        }
        gestureFliperView.setOnFacousChangedListener(new FlipperFacousChangedListener() {

            @Override  //实现监听接口
            public void onFliperChanged(int index) {
                // TODO Auto-generated method stub
                for (int i = 0; i < mImageView.length; i++) {
                    if (i == index) {
                        mImageView[i].setImageResource(R.drawable.page_selected);
                    } else {
                        mImageView[i].setImageResource(R.drawable.page_normal);
                    }

                }
            }
        });
        return mainLayout;
    }

}*/

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @Override
    public Toolbar getToolbar() {
        return null;
    }

    /**
     * Glide框架加载图片
     *
     * @param urlString
     * @return
     */
    private ImageView getImageViewLoadURL(String urlString) {
        Log.i(TAG, "getImageViewLoadURL: " + urlString);
        ImageView imageView = new ImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(mContext).load(urlString).into(imageView);
        return imageView;
    }

    /**
     *
     */
    public class LocalImageHolderView implements Holder<String> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, final int position, String data) {
            //imageView.setImageResource(data);
            Glide.with(mContext).load(data).into(imageView);
        }
    }
}
